﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Build.Utilities;

namespace CommonTools
{
    [Flags]
    public enum TFCommands
    {
        None = 0,
        Get = 1,
        CheckOut = 2,
        CheckIn = 4,
        Undo = 8,
        All = 16
    }
    
    public class TFHelper : CommandLineHelper
    {
        private string _tfPath;
        public string TFPath 
        {
            get 
            { 
                if (_tfPath == null) 
                    _tfPath = Path.Combine(GetVisualStudioPath(), "tf.exe");
                
                return _tfPath;
            }
            set { _tfPath = value; }
        }

        public TFCommands SupportedCommands = TFCommands.None;

        public override void Execute(string appPath, string args)
        {
            base.Execute(appPath, args);
        }
        public override void AddToOutput(string info, ref string output)
        {
            base.AddToOutput(info, ref output);
        }

        public string Execute(TFCommands command, string fileName)
        {
            ClearOutput();
            
            if (SupportedCommands.HasFlag(command))
            {
                AddToOutput(GetMessage(command) + " " + fileName);
                Execute(TFPath, command.ToString() + " " + Quote(fileName));
            }
            return Output;
        }

        private string GetMessage(TFCommands command)
        {
            if (command == TFCommands.Get)
                return "Getting";
            if (command == TFCommands.CheckOut)
                return "Checking out";
            if (command == TFCommands.CheckIn)
                return "Checking in";
            if (command == TFCommands.Undo)
                return "Undoing";
            return "";
        }

        public string Get(string fileName)
        {
            return Execute(TFCommands.Get, fileName);
        }

        public string CheckOut(string fileName)
        {
            return Execute(TFCommands.CheckOut, fileName);
        }

        public string CheckIn(string fileName)
        {
            return Execute(TFCommands.CheckIn, fileName);
        }

        public string Undo(string fileName)
        {
            return Execute(TFCommands.Undo, fileName);
        }

        public static string GetVisualStudioPath()
        {
            string[] versions = Properties.Settings.Default.VisualStudioVersions.Split(',');

            string vsPath = "";

            for (int i = 0; i < versions.Length; i++)
            {
                string vsToolsPath = Environment.GetEnvironmentVariable(string.Format("VS{0}COMNTOOLS", versions[i]), EnvironmentVariableTarget.Machine);
                if (!string.IsNullOrEmpty(vsToolsPath))
                    vsPath = new DirectoryInfo(vsToolsPath).Parent.FullName + "\\IDE";
            }

            return vsPath;
        }
    }

}
